/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.serializer.changeser.entity.serializer;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.spi.jsonser.util.SerializerUtil;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

public class IsChildObjectCodeGenerator extends ClassMethodGenerator {

    private static final String methodName = "isChildObjectCode";
    private static final String paramPropertyName = "propertyName";
    private IGspCommonObject object;

    public IsChildObjectCodeGenerator(IGspCommonObject object) {
        this.object = object;
    }

    @Override
    protected Block buildMethodBody() {
        Block block = ast.newBlock();
        buildIfStatements(block);
        buildReturnFalseStatement(block);
        return block;
    }

    private void buildReturnFalseStatement(Block block) {
        ReturnStatement ifBodyReturnStatement = ast.newReturnStatement();
        BooleanLiteral booleanLiteral = ast.newBooleanLiteral(false);
        ifBodyReturnStatement.setExpression(booleanLiteral);
        block.statements().add(ifBodyReturnStatement);
    }

    private void buildIfStatements(Block block) {
        for (IGspCommonObject childObj : object.getContainChildObjects()) {
            IfStatement returnStatement = getIfStatement(getPropertyName(childObj.getCode()));
            block.statements().add(returnStatement);
        }
    }

    private IfStatement getIfStatement(String key) {
        IfStatement ifStatement = ast.newIfStatement();
        setInfixExpression(key, ifStatement);
        setThenStatement(ifStatement);
        return ifStatement;
    }

    private void setThenStatement(IfStatement ifStatement) {
        Block block = ast.newBlock();
        ReturnStatement ifBodyReturnStatement = ast.newReturnStatement();
        BooleanLiteral booleanLiteral = ast.newBooleanLiteral(true);
        ifBodyReturnStatement.setExpression(booleanLiteral);
        block.statements().add(ifBodyReturnStatement);
        ifStatement.setThenStatement(block);
    }

    private void setInfixExpression(String key, IfStatement ifStatement) {
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        methodInvocation.setName(ast.newSimpleName("isChildCodeEquals"));
        methodInvocation.setExpression(GeneratorUtil.buildQualifiedName(ast, SerializerUtil.class));
        methodInvocation.arguments().add(ast.newSimpleName(paramPropertyName));
        StringLiteral stringLiteral = ast.newStringLiteral();
        stringLiteral.setLiteralValue(key);
        methodInvocation.arguments().add(stringLiteral);

        ifStatement.setExpression(methodInvocation);
    }

    private String getPropertyName(String nodeCode) {
        return String.format("%1$s%2$s", nodeCode, "s");
    }

    @Override
    protected boolean getIsOverride() {
        return true;
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);

        return list;
    }

    @Override
    protected String getMethodName() {
        return methodName;
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        ArrayList<ParameterInfo> list = new ArrayList<ParameterInfo>();
        ParameterInfo parameterInfo = new ParameterInfo();
        parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
        parameterInfo.setParamName(paramPropertyName);
        list.add(parameterInfo);
        return list;
    }

    @Override
    protected TypeInfo getReturnType() {
        return new TypeInfo(boolean.class.getTypeName());
    }
}
